{% extends 'layout.html' %}

{% block main %}
<div class="page-header">
    <h1>
        SVN关联信息
    </h1>
</div><!-- /.page-header -->
<div class="row">
    <div class="col-xs-12">
        <div class="row" style="margin-bottom: 10px;">
            <div class="col-xs-12">
                SVN路径:{{ci.svn_path}}
            </div>
        </div>
        <div class="row" style="margin-bottom: 10px;">
            <div class="col-xs-12">
                <span style="font-size: 20px;">
                    人员列表
                </span>
            </div>
        </div>
        <div class="row" style="margin-bottom: 10px;">
            <div class="col-xs-12">
                <div class="navbar-header" style="margin-right: 18px;">
                    <span class="navbar-brand">姓名</span>
                </div>
                <div class="nav navbar-nav form-group">
                    <div class="navbar-form navbar-left">
                        <div class="form-group">
                            <select id="newOperator" style="width:150px;" class="select2 user-select2 form-control" data-placeholder="搜索姓名">
                            </select>
                            <button class="btn btn-sm btn-success"
                                onclick="createRole({{ci_id}})">新增成员</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12">
                <table class="table table-bordered table-striped table-condensed"
                    style="table-layout: fixed">
                    <thead>
                        <tr>
                            <th style="text-align: center; width: 100px;">id</th>
                            <th style="text-align: center; width: 100px;">姓名</th>
                            <th style="text-align: center; width: 120px;">域账号</th>
                            <th style="text-align: center;">邮箱</th>
                            <th style="text-align: center; width: 195px;">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for user in users %}
                        <tr>
                            <td><div style="text-align: center; white-space: nowrap;">{{user._id}}</div></td>
                            <td><div style="text-align: center; white-space: nowrap;">{{user.operator_name}}</div></td>
                            <td><div style="text-align: center; white-space: nowrap;">{{user.login_name}}</div></td>
                            <td><div style="text-align: center; white-space: nowrap;">{{user.operator_email}}</div></td>
                            <td><div style="text-align: center; white-space: nowrap;">
                                    <button class="btn btn-xs btn-success"
                                        onclick="showOperatorRelation({{user._id}})">关联关系</button>
                                    <button class="btn btn-xs btn-primary"
                                        onclick="editOperator({{user._id}})">编辑</button>
                                    <button class="btn btn-xs btn-danger"
                                        onclick="deleteRelation({{ci_id}},{{user._id}})">删除关联</button>
                                </div></td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
        <div class="row" style="margin-bottom: 10px;">
            <div class="col-xs-12">
                <span style="font-size: 20px;">
                    操作日志
                </span>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12">
                <table class="table table-bordered table-striped table-condensed"
                    style="table-layout: fixed">
                    <thead>
                        <tr>
                            <th style="text-align: center; width: 50px;">ID</th>
                            <th style="text-align: center; width: 200px;">操作者</th>
                            <th style="text-align: center; width: 120px;">操作类型</th>
                            <th style="text-align: center; width: 150px;">操作时间</th>
                            <th style="text-align: center;">详细信息</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for log in logs %}
                        <tr>
                            <td><div style="text-align: center; white-space: nowrap;">{{log.id}}</div></td>
                            <td><div style="text-align: center; white-space: nowrap;">{{log.operator}}</div></td>
                            <td><div style="text-align: center; white-space: nowrap;">{{log.command}}</div></td>
                            <td><div style="text-align: center; white-space: nowrap;">{{log.create_time}}</div></td>
                            <td><div style="text-align: center; white-space: nowrap;">{{log.content}}</div></td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

{% endblock %}

{% block script %}
<script src="/static/js/chosen.jquery.min.js"></script>
<script src="/static/js/jquery-ui-1.10.3.full.min.js"></script>
<script src="/static/js/jquery.ui.touch-punch.min.js"></script>
<script type="text/javascript" src="/static/js/select2.min.js"></script>
<script src="/static/js/all-config.js"></script>
<script>
    var editOperator = function(ciId){
        window.open("/cmdb/ci/edit?type=operator&id=" + ciId);
    };
    var showOperatorRelation = function(ciId){
        window.open("/cmdb/relation?type=operator&id=" + ciId);
    };
    var createRole = function(svn_id){
        var saveData = {
            'svnId': svn_id,
            'user': $('#newOperator').val()
        };
        $.ajax({
            url: "/cmdb/operator/createSvnUser",
            type: "POST",
            data: JSON.stringify(saveData),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                if (data.redirect) {
                    window.location.href = data.redirect;
                    return;
                }
                if (data.success){
                    location.reload(true);
                }else{
                    alert("新增关联失败");
                }
            }
        });
    };

    var deleteRelation = function(first_id, second_id){
        ret = window.confirm("确认要删除关联关系?")
        if (!ret) {
            return;
        }

        var saveData = {
            'first_id': first_id,
            'second_id': second_id
        };
        $.ajax({
            url: "/cmdb/relation/delete",
            type: "POST",
            data: JSON.stringify(saveData),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                if (data.redirect) {
                    window.location.href = data.redirect;
                    return;
                }
                if (data.success){
                    location.reload(true);
                }else{
                    alert("删除关联失败");
                }
            }
        });
    };

    var user_select2_option = {
        ajax: {
            url: "/util/user",
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    q: params.term
                };
            },
            processResults: function (data, page) {
                return {
                    results: data.items
                };
            },
            cache: true
        },
        allowClear: true,
        minimumInputLength: 2
    };
    $(".user-select2").select2(user_select2_option);
    delete user_select2_option['allowClear'];
    jQuery(function($){
    });
</script>
{% endblock %}